require.config({
    baseUrl: "../modules",
    paths: {
        jq: "../libs/jquery.2.2.4",
        getData: "getData",
        //商品渲染
        render: "cartRender",
        //单个勾选
        SingleChoice: "SingleChoice",
        //全选
        MultipleChoice: "MultipleChoice",
        //计算商品总数量
        numNum: "numNum",
        //计算数量
        quantity: "quantity",
        //计算价格
        Calculateprice: "Calculateprice",
        //计算勾选中的总价
        Totalprice: "Totalprice",
        //删除单个商品
        delt: "delete",
        //删除已勾选的商品
        deleteAll: "deleteAll"
    }
})
require(["jq", "getData", "render", "SingleChoice", "MultipleChoice", "numNum", "quantity", "Calculateprice", "Totalprice", "delt", "deleteAll"], function (_, getData, render, SingleChoice, MultipleChoice, numNum, quantity, Calculateprice, Totalprice, delt, deleteAll) {
    // console.log(dtAll())
    //获取本地存储中的数据
    let goods = localStorage.getItem("goods") ? JSON.parse(localStorage.getItem("goods")) : [];
    //商品数据路径
    const url = "http://localhost:3000/api?type=getGoods";
    //开启ajax请求商品数据
    getData(url).then((res) => {
        //渲localstorage数据
        render(res, goods, $(".cont"))
        //单选
        SingleChoice($(".btn"), goods)
        //默认有勾的计算总数量
        $(".piece").html(numNum(goods));
        //默认有勾的进行总价格计算
        // Totalprice(goods, $(".Subtotal"))
        // 计算加减数量
        quantity(goods, $(".znum"))
        // 计算加减数量
        //右增
        $(".cont").on("click", ".plus", function () {
            for (let i = 0; i < goods.length; i++) {
                if (goods[i].id === $(this).parent().parent().attr("id")) {
                    goods[i].num++
                    $(this).prev().html(goods[i].num)
                    localStorage.setItem("goods", JSON.stringify(goods))
                }
            }
        })
        //左减
        $(".cont").on("click", ".reduce", function () {
            console.log()
            for (let i = 0; i < goods.length; i++) {
                if (goods[i].id === $(this).parent().parent().attr("id")) {
                    goods[i].num--
                    if (goods[i].num <= "1") {
                        goods[i].num = 1
                    }
                    $(this).next().html(goods[i].num)
                    localStorage.setItem("goods", JSON.stringify(goods))
                }
            }
        })
        //点btn计算总数量
        $(".cont").on("click", ".btn", function () {
            //根据点中的是否为勾在进行总数量计算
            $(".piece").html(numNum(goods))
            //根据点中的是否为勾在进行总价格计算
            Totalprice(goods, $(".Subtotal"))
        })

        //点击单个数量总数量和总价格实时更新
        $(".cont").on("click", ".num", function () {
            //计算总数量
            $(".piece").html(numNum(goods))
            //计算价格
            Calculateprice($(".Subtotal"), $(".znum"), $(".money"))
            //计算总价
            Totalprice(goods, $(".Subtotal"))
        })

        //点击单个商品删除
        $(".cont").on("click", ".del", function () {
            delt(goods, $(this))
            // //计算总数量
            $(".piece").html(numNum(goods))
            // //计算总价
            Totalprice(goods, $(".Subtotal"))
        })
        // 点删除以选中的商品
        $(".cards-b").on("click", ".deltAll", function () {
            //删除
            deleteAll(goods, $(".icon-gouxuankuang1"))
            $(".piece").html(0)
            $(".dolr").html("¥0")
        })
        //全选
        $(".cart1").on("click", ".allbtn", function () {
            MultipleChoice($(".allbtn"), $(".btn"), goods)
            // //计算总数量
            $(".piece").html(numNum(goods))
            // //计算总价
            Totalprice(goods, $(".Subtotal"))
        })
    })
})
